package me.f1reking.serialportlib;

import android.util.Log;

import java.io.IOException;

import me.f1reking.serialportlib.entity.TemporarySerialHexMessage;
import me.f1reking.serialportlib.listener.ISerialPortDataListener;

public class SerialMessageHandleThread extends Thread{
    private TemporarySerialHexMessage temporarySerialHexMessage;
    private ISerialPortDataListener mISerialPortDataListener;
    private String hexMessage=null;
    private int serialCodeInterval=64;
    public void onReceiveMessage(String hexMessage){
        this.hexMessage=hexMessage;
    }



    public void setISerialPortDataListener(ISerialPortDataListener iSerialPortDataListener) {
        this.mISerialPortDataListener = iSerialPortDataListener;
    }

    @Override
    public void run() {
        super.run();
        while (!isInterrupted()){
            if (temporarySerialHexMessage == null) {
                temporarySerialHexMessage = new TemporarySerialHexMessage();
                temporarySerialHexMessage.setCode(hexMessage);
                temporarySerialHexMessage.setLastTime(System.currentTimeMillis());
            } else {
                long dur = System.currentTimeMillis() - temporarySerialHexMessage.getLastTime();

                if (hexMessage!=null&&hexMessage.length() > 0) {
                    if (dur <= serialCodeInterval) {
                        //小于接收间隔，拼接码
                        String receiveCode = (temporarySerialHexMessage.getCode()!=null?temporarySerialHexMessage.getCode():"") + hexMessage;
                        temporarySerialHexMessage = new TemporarySerialHexMessage();
                        temporarySerialHexMessage.setCode(receiveCode);
                        temporarySerialHexMessage.setLastTime(System.currentTimeMillis());
                    } else {
                        //大于接收间隔，将上一次接收的数据回调
                        String receiveCode = temporarySerialHexMessage.getCode();
                        if (receiveCode!=null&&receiveCode.length() > 0) {
                            if(mISerialPortDataListener!=null){
                                mISerialPortDataListener.onDataReceived(receiveCode);
                            }
                        }
                        temporarySerialHexMessage = new TemporarySerialHexMessage();
                        temporarySerialHexMessage.setCode(hexMessage);
                        temporarySerialHexMessage.setLastTime(System.currentTimeMillis());
                    }
                } else {
                    if (dur >= serialCodeInterval) {
                        String receiveCode = temporarySerialHexMessage.getCode();
                        if (receiveCode!=null&&receiveCode.length() > 0) {
                            if(mISerialPortDataListener!=null){
                                mISerialPortDataListener.onDataReceived(receiveCode);
                            }
                        }
                        temporarySerialHexMessage = new TemporarySerialHexMessage();
                        temporarySerialHexMessage.setCode(null);
                        temporarySerialHexMessage.setLastTime(System.currentTimeMillis());
                    }
                }
                hexMessage=null;
            }
        }

    }

    /**
     * 释放
     */
    public void release() {
        interrupt();
    }
}
